'''
Created on Sep 17, 2010

@author: diego
'''
import cv
from siftTracking import compare_desc


def compare_desc(desc1,desc2):
    a=zip(desc1,desc2)
    sum=0
    for (x,y) in a:
        sum=sum + abs(x-y)
    return sum

def drawKeypoints(image,keyp):
    for ((x, y), laplacian, size, dir, hessian) in keyp:
        cv.Circle(image, (x,y), size, (200,50,50))


if __name__ == '__main__':
    storage=cv.CreateMemStorage(0)
    params=(0,3000,3,1)
    book1=cv.LoadImage("../resources/ontheroad.jpg",cv.CV_LOAD_IMAGE_GRAYSCALE)
    book2=cv.LoadImage("../resources/ontheroad2.jpg",cv.CV_LOAD_IMAGE_GRAYSCALE)
    match=cv.LoadImage("../resources/ontheroad.jpg")
    match2=cv.LoadImage("../resources/ontheroad2.jpg")
    
    (keyp,desc)=cv.ExtractSURF(book1, None, storage, params)
    (keyp2,desc2)=cv.ExtractSURF(book2, None, storage, params)
    drawKeypoints(book1,keyp)
    drawKeypoints(book2,keyp2)
    cv.ShowImage("book1", book1)
    cv.ShowImage("book2", book2)
    i=0
    for d in desc:
        j=0
        for e in desc2:
            Nearest_Neighbor_Threshold = 1.0
            if compare_desc(d, e)<Nearest_Neighbor_Threshold:
                drawKeypoints(match, [keyp[i]])
                drawKeypoints(match2, [keyp2[j]])
            j=j+1
        i=i+1        
    
    cv.ShowImage("match", match)
    cv.ShowImage("match2", match2)
    cv.WaitKey(0)
    
#    compare_desc(desc, desc2)
#        for ((x, y), laplacian, size, dir, hessian) in keypoints:
#            print "x=%d y=%d laplacian=%d size=%d dir=%f hessian=%f" % (x, y, laplacian, size, dir, hessian)
#    cv.Circle(frame, (x,y), size, (200,50,50))
    

